<?php
class Content extends Admin_Controller {
 
    public function __construct() 
    {
        parent::__construct();
        
        $this->load->model('post_model');
        
        $this->load->model('user_model');
        
         $this->load->model('pay_model');
        
        $this->load->library('blog_helper');
 
        Template::set('toolbar_title', 'Manage Your Blog');
        
        Template::set_block('sub_nav', 'content/sub_nav');
    }
 
    //--------------------------------------------------------------------
 
    public function index() 
    {
    	$posts = $this->post_model->where('deleted', 0 )->find_all();
 
		Template::set('posts', $posts);
 
        Template::render();
    }
 
    //--------------------------------------------------------------------
 
	public function create() 
	{
	    if ($this->input->post('submit'))
	    {
	        if ($this->save_post())
	        {
	            Template::set_message('You post was successfully saved.', 'success');
	            redirect(SITE_AREA .'/content/blog');
	        }
	    }
	 
	    Template::set('toolbar_title', 'Create New Post');

	    Template::set_view('content/post_form');
	    
	    Template::render();
	}
	
	//----------------------------------------------------------------------
	
	private function save_post($type='insert', $id=null) 
	{
	    $this->form_validation->set_rules('title', 'Title', 'required|trim|strip_tags|xss_clean');

	    $this->form_validation->set_rules('url', 'Url', 'trim|strip_tags|xss_clean');
	    
	    $this->form_validation->set_rules('description', 'Description', 'required|trim|strip_tags|xss_clean');
	 
	    if ($this->form_validation->run() === false)
	    {
	        return false;
	    }
	 
	    // Compile our post data to make sure nothing
	    // else gets through.
	    $data = array(
	        'title' 		 => $this->input->post('title'),
	        'url'    		 => $this->input->post('url'),
	        'description'    => $this->input->post('description')
			
	    );
	 
	    if ($type == 'insert')
	    {
	        $return = $this->post_model->insert($data);
	    }
	    else   // Update
	    {
	        $return = $this->post_model->update($id, $data);
	    }
	 
	    return $return;
	}
	
	//--------------------------------------------------------------------------------
	
	public function edit_post($id=null) 
	{
	    if ($this->input->post('submit'))
	    {
	        if ($this->save_post('update', $id))
	        {
	            Template::set_message('You post was successfully saved.', 'success');
	            redirect(SITE_AREA .'/content/blog');
	        }
	    }
	 
	    Template::set('post', $this->post_model->find($id));
	 
	    Template::set('toolbar_title', 'Edit Post');
	    
	    Template::set_view('content/post_form');
	    
	    Template::render();
	}
	
	//----------------------------------------------------------------------------------
	
	public function user_details()
	{
		$users = $this->user_model->find_all();
		$user_detail = array();
		if(!empty($users))
		{
			foreach ($users as $user) 
			{
				$data = array();
				$data['user_id'] = $user->id;
				$data['username'] = $user->username;
				$data['user_name'] = $user->display_name;
				$data['user_likes'] = $this->blog_helper->get_user_total_likes_by_user_id($user->id);
				$data['user_amount'] = $this->blog_helper->user_total_amount($user->id);
				array_push($user_detail, $data);
			}
		}
		Template::set('user_detail',$user_detail);
		Template::render();
	}

	//----------------------------------------------------------------------------------
	
	public function pay_detail($id)
	{
		$data['user'] = $this->user_model->find($id);
		$data['pay_detail'] = $this->pay_model->find_by('user_id',$id);
		$data['total_amount'] = $this->blog_helper->user_total_amount($id);
		
		Template::set('pay_detail',$data);
		Template::render();
	}

	//----------------------------------------------------------------------------------
	
	public function pay_amount($user_id,$amount)
	{
		$user = $this->user_model->find($user_id);
		if(!empty($user))
		{
			$user_pay_detail = $this->pay_model->find_by('user_id',$user_id);
			if(!empty($user_pay_detail))
			{
				if($this->pay_model->update($user_pay_detail->id,array('total_paid'=>($user_pay_detail->total_paid+$amount))))
				{
					echo true;
				}
				else 
				{
					echo false;
				}
			}
			else 
			{
				$data = array(
					'user_id' => $user_id,
					'total_paid' => $amount
				);
				
				if($this->pay_model->insert($data))
				{
					echo true;
				}
				else 
				{
					echo false;
				}
			}
		}
		else 
		{
			echo false;
		}
		
	}
}